﻿using System.Collections.Generic;
using System.IO;
using LitJson;

public static class SaveHelper
{
    public static bool IsFileExist(string filePath)
    {
        return File.Exists(filePath);
    }

    public static bool IsDirectoryExists(string filePath)
    {
        return Directory.Exists(filePath);
    }

    public static void CreateFile(string fileName, string content)
    {
        StreamWriter streamWriter = File.CreateText(fileName);
        streamWriter.Write(content);
        streamWriter.Close();
    }

    public static void CreateDirectory(string filePath)
    {
        if (IsDirectoryExists(filePath))
        {
            return;
        }
        Directory.CreateDirectory(filePath);
    }

    private static string SerializeObject(object pObject)
    {
        string serializedString = string.Empty;
        serializedString = JsonMapper.ToJson(pObject);
        return serializedString;
    }

    private static object DeserializeObject<pType>(string pString)
    {
        object deserializedObject = null;
        deserializedObject = JsonMapper.ToObject<pType>(pString);
        return deserializedObject;
    }

    public static void SaveData(string fileName, object pObject)
    {
        // 如果文件已存在，则删除
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        string toSave = SerializeObject(pObject);
        toSave = Utils.RijndaelEncrypt(toSave);
        StreamWriter streamWriter = File.CreateText(fileName);
        streamWriter.Write(toSave);
        streamWriter.Close();
    }

    public static object ReadData<pType>(string str, bool isFile = true)
    {
        string data;
        if (isFile)
        {
            // 如果文件不存在，则返回空
            if (!File.Exists(str))
            {
                return null;
            }
            StreamReader streamReader = File.OpenText(str);
            data = streamReader.ReadToEnd();
            streamReader.Close();
        }
        else
        {
            data = str;
        }

        data = Utils.RijndaelDecrypt(data);
        return DeserializeObject<pType>(data);
    }
    #region CSV操作
    /// <summary>
    /// 利用list模拟表格
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="fullPath"></param>
    public static void SaveCSV(List<LTLocalizationData> dt, string fullPath)//table数据写入csv  
    {
        if (dt.Count == 0) return;
        
        System.IO.FileInfo fi = new System.IO.FileInfo(fullPath);
        if (!fi.Directory.Exists)
        {
            fi.Directory.Create();
        }
        System.IO.FileStream fs = new System.IO.FileStream(fullPath, System.IO.FileMode.Create,
            System.IO.FileAccess.Write);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
        string data = "KEY,";

        for (int i = 0; i < dt.Count; i++)//写入列名
        {
            data += dt[i].LanguageType;
            if (i < dt.Count - 1)
            {
                data += ",";
            }
        }
        sw.WriteLine(data);

        List<string> tempKeys = new List<string>(dt[0].LanguageData.Keys);
        for (int i = 0; i < tempKeys.Count; ++i)
        {
            data = tempKeys[i]+",";
            for (int j = 0; j < dt.Count; j++) //写入各行数据  
            {
                string str = dt[j].LanguageData[tempKeys[i]];
                str = str.Replace("\"", "\"\"");//替换英文冒号 英文冒号需要换成两个冒号  
                if (str.Contains(",") || str.Contains("\"")
                    || str.Contains("\\r") || str.Contains("\\n")) //含逗号 冒号 换行符的需要放到引号中  
                {
                    str = string.Format("\"{0}\"", str);
                }
                data += str;
                if (j < dt.Count - 1)
                {
                    data += ",";
                }
            }
            sw.WriteLine(data);
        }
        //for (int i = 0; i < dt.Count; i++) //写入各行数据  
        //{
        //    data = "";
        //    for (int j = 0; j < dt[i].Count; j++)
        //    {
        //        string str = dt[i][j].ToString();
        //        str = str.Replace("\"", "\"\"");//替换英文冒号 英文冒号需要换成两个冒号  
        //        if (str.Contains(",") || str.Contains("\"")
        //            || str.Contains("\\r") || str.Contains("\\n")) //含逗号 冒号 换行符的需要放到引号中  
        //        {
        //            str = string.Format("\"{0}\"", str);
        //        }

        //        data += str;
        //        if (j < dt[i].Count - 1)
        //        {
        //            data += ",";
        //        }
        //    }
        //    sw.WriteLine(data);
        //}
        sw.Close();
        fs.Close();
    }
    #endregion
}
